// TOWN SCRIPT
//    Town 1: Proving Grounds - First Level

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 1, 0 = Murphy's ghost encounter (set during ghost creation)
// 1, 27 = balanced pre-sets
// 1, 28 = presets all gone
// 1, 29 = generate new monsters

begintownscript;

variables;

short i, j, x, y, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(1);

	// reset decrement memory cell for murphy's ghosts...
	// * only when saved and reloaded on first level - otherwise does nothing
	i = 86;
	j = 0;
	while (i < 120) {
		if ((char_ok(i) == TRUE) && (creature_type(i) == 242)) {
			set_creature_memory_cell(i, 1, 1);
			j = 1;
		}
		i = i + 1;
	}
	// no murphy's ghosts, allow for them again then...
	if (j == 0) {
		set_flag(1, 0, 0);
	}

	// reset murphy ghost encounter if no longer available
	if (get_flag(1, 0) == 0) {
		set_flag(1, 3, 0);
	}

	// check monsters...
	run_scenario_script(15);
	if (get_flag(1, 29) > 0) {
		// generate some more...
		set_flag(1, 29, 0);
		print_str_color("Generating monsters...", 3);
		j = get_ran(1, 14, 24);
		i = 0;
		while (i < j) {
			run_scenario_script(27);
			if (get_flag(100, 0) > 0) {
				run_scenario_script(26);
				place_monster(get_flag(100, 1), get_flag(100, 2), get_flag(100, 0), 0);
			}
			i = i + 1;
		}
		run_scenario_script(36);
	}
break;

beginstate EXIT_STATE;
	// Always called when the town is left.
break;

beginstate START_STATE;
break;

beginstate 10;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading back to the castle.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb up");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder back to the castle...
		move_to_new_town(0, 11, 10);
	}
break;

beginstate 11;
	if (get_flag(1, 3) == 0) {
		set_flag(1, 3, 1);
		if (get_flag(1, 0) == 0) {
			reset_dialog();
			add_dialog_str(0, "As you enter the room, you see an image of a hooded humanoid.", 0);
			add_dialog_str(1, "There is a golden light coming from a hole in the hood.", 0);
			add_dialog_str(2, "The apparition is bejeweled with precious and semiprecious stones.", 0);
			add_dialog_str(3, "In front of the spectre is an altar, from which fresh incense is burning.", 0);
			add_dialog_str(4, "The image fades leaving behind two entities.", 0);
			add_dialog_choice(0, "OK");
			choice = run_dialog(1);
			// set two ghosts in room...
			set_flag(1, 0, 2);
			place_monster(31, 33, 242, 0);
			set_creature_memory_cell(char_on_spot(31, 33), 1, 1);
			// memory cell two stays at zero...
			place_monster(31, 34, 242, 0);
			set_creature_memory_cell(char_on_spot(31, 34), 1, 1);
		}
	}
break;

beginstate 12;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading down into the maze.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb down");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder down...
		move_to_new_town(2, 27, 29);
	}
break;

beginstate 13;
	block_entry(1);
	x = char_loc_x(0);
	y = char_loc_y(0);
	if (y >= 44) {
		y = 5;
	}
	else {
		y = 43;
	}
	teleport_party(x, y, 1);
break;

beginstate 14;
	block_entry(1);
	teleport_party(36, 36, 1);
break;

beginstate 15;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a funny looking door here.", 0);
	add_dialog_str(1, "Go through it?", 0);
	add_dialog_choice(0, "Stay here");
	add_dialog_choice(1, "Go through");
	choice = run_dialog(0);
	if (choice == 2) {
		play_sound(-58);
		teleport_party(4, 26, 1);
		play_sound(59);
	}
break;

beginstate 16;
	if (get_flag(1, 1) == 0) {
		reset_dialog();
		add_dialog_str(0, "In this room is a silver statue of a boar with horns and long fangs.", 0);
		add_dialog_str(1, "On the wall by the statue is a message (parially obscured) that appears to have been left by passing elves.", 0);
		add_dialog_str(2, "It is hardly legible, but some comments warning about ghosts and demons can still be made out.", 0);
		add_dialog_str(3, "There is a silver key here.", 0);
		add_dialog_choice(0, "Leave it");
		add_dialog_choice(1, "Take it");
		choice = run_dialog(1);
		if (choice == 2) {
			set_flag(1, 1, 1);
			change_spec_item(3, 1);
		}
	}
	else {
		message_dialog("You search the statue but find nothing new.", "");
	}
break;

beginstate 17;
	if (get_flag(1, 2) == 0) {
		reset_dialog();
		add_dialog_str(0, "On the wall is an image of a strange beast with the body of a chicken and the head of a cat.", 0);
		add_dialog_str(1, "There are unusual runes on a plaque underneath.", 0);
		add_dialog_str(2, "There is also a bronze key here.", 0);
		add_dialog_choice(0, "Leave it");
		add_dialog_choice(1, "Take it");
		choice = run_dialog(1);
		if (choice == 2) {
			set_flag(1, 2, 1);
			change_spec_item(2, 1);
		}
	}
	else {
		message_dialog("There is an image of a strange beast on the wall.", "There is nothing else here.");
	}
break;

beginstate 18;
	reset_dialog();
	add_dialog_str(0, "The teleporter crackles and hums casting an eerie light.", 0);
	add_dialog_str(1, "You see a faint image of the castle within it's shimmering glow.", 0);
	add_dialog_str(2, "You also faintly hear the words 'Mapiro Mahama Diromat' coming from the teleporter.", 0);
	add_dialog_str(3, "You can just imagine a small man in long robes intoning those words self importantly.", 0);
	add_dialog_choice(0, "OK");
	choice = run_dialog(1);
break;

beginstate 19;
	block_entry(1);
	reset_dialog_preset_options(3);
	choice = run_dialog(1);
	if (choice == 2) {
		i = 0;
		while (i < 4) {
			put_sparkles_on_char(i, 0, 12);
			i = i + 1;
		}
		run_animation_sound(10);
		move_to_new_town(0, 24, 19);
	}
break;

beginstate 20;
	reset_dialog();
	add_dialog_str(0, "There is an elevator here.", 0);
	add_dialog_str(1, "Use?", 0);
	add_dialog_choice(0, "No");
	add_dialog_choice(1, "Yes");
	choice = run_dialog(0);
	if (choice == 2) {
		block_entry(1);
		i = 0;
		while (i == 0) {
			get_text_response("Which level (A-D), A exits");
			check_text_response_match("A");
			if (got_text_match() > 0) {
				block_entry(0);
				end();
			}
			check_text_response_match("B");
			if (got_text_match() > 0) {
				move_to_new_town(2, 24, 26);
				end();
			}
			check_text_response_match("C");
			if (got_text_match() > 0) {
				move_to_new_town(3, 23, 26);
				end();
			}
			check_text_response_match("D");
			if (got_text_match() > 0) {
				move_to_new_town(4, 23, 26);
				end();
			}
		}
	}
break;
